var conData = require('MinesConstants');
var logic = require('Logic');
var HexMine = require('HexMine');
cc.Class({
    extends: cc.Component,

    properties: {
        minePrefab:
        {
            default:null,
            type:cc.Prefab
        },
        itemLayerNode:cc.Node
    },

    // use this for initialization
    onLoad: function () {
        this.initData();
        this.initView();
    },

    initData:function()
    {
        this.minesNum =  parseInt(Math.random()*15) + 15;
        this.boardArray = logic.generBoardData(this.minesNum);

        //var s=[[1,2],[2,3],3,5,6,9];
        //var u= logic.contains(s,[2,3]);
        //console.log('---------->'+u);
    },


    initView:function()
    {
        var startPos = cc.p(-304,410);
        var disPos = cc.p(conData.DIS_MINE_COL,conData.DIS_MINE_ROW);
        for(var i=0;i<this.boardArray.length;i++)
        {
            var lis = this.boardArray[i];
            var offsetX = ( i % 2 == 0 ? 0 : 38);
            for(var j=0;j<lis.length;j++)
            {
                var nextPos = cc.pAdd(startPos,cc.p(offsetX+j*disPos.x,-i*disPos.y));
                var mine = cc.instantiate(this.minePrefab);
                mine.setPosition((nextPos));
                this.node.addChild(mine,10-j+i*10,i*10+j);
                var hexMine =  mine.getComponent(HexMine);
                hexMine.setCoord(i,j,lis[j]);
                if(lis[j] == -1)
                {
                    //hexMine.mineSp.enabled = true;
                }

                var eventHandler = new cc.Component.EventHandler();
                eventHandler.target = this.node;
                eventHandler.component = this.__proto__.__classname__ ;
                eventHandler.handler = 'mineTouchCallBack';
                hexMine.clickEvents.push(eventHandler);
            }
        }
    },


    mineTouchCallBack:function(event)
    {
        var hexMine = event.target.getComponent(HexMine);
        var curSec = new Date().getTime();

        //console.log("--------->"+hexMine.touchBeganTime_msec+"    "+curSec);
        if( hexMine.isMarked() || curSec - hexMine.touchBeganTime_msec > 300)
        {
            if(hexMine.isMarked())
            {
                this.minesNum++;
            }
            else
            {
                this.minesNum -- ;
            }
            //标记炸弹
            hexMine.mark();
            this.itemLayerNode.getComponent(require('ItemLayer')).updateMinesNum();
        }
        else
        {
            var addShowLis = [];
            var isTouchMine = logic.isTouchInMine(hexMine.row,hexMine.col,this.boardArray,addShowLis);
            this.updateBoard(addShowLis);
        }



        //console.log(hexMine.row+"  "+hexMine.col + " "+ addShowLis.length);
        //
        //for(var i=0;i<addShowLis.length;i++)
        //{
        //    console.log("--------->"+addShowLis[i][0]+"    "+addShowLis[i][1]);
        //}
    },


    //touchMine:function(hexMine)
    //{
    //    hexMine.
    //},

    updateBoard:function(addShowList)
    {
        for(var i=0;i<addShowList.length;i++)
        {
            var tag = addShowList[i][0]*10+addShowList[i][1];
            var hexNode = this.node.getChildByTag(tag);
            if(hexNode)
            {
                var hexMine = hexNode.getComponent(HexMine);
                if(hexMine.isOpen == false)
                {
                    hexMine.open();
                }
                if(hexMine.state == conData.MINE)
                {
                    this.minesNum--;
                    this.itemLayerNode.getComponent(require('ItemLayer')).updateMinesNum();
                }
            }
        }
    },


    // called every frame, uncomment this function to activate update callback
    // update: function (dt) {

    // },
});
